#define _CRT_SECURE_NO_WARNINGS 1		
#include "SqNode.h"

DSL* DSqListInit() {
    DSL* phead = BuyDSqListNode(0);
    phead->next = phead;
    phead->prev = phead;
    return phead;
}

DSL* BuyDSqListNode(DSqListType x) {
    DSL* newnode = (DSL*)malloc(sizeof(DSL));
    if (newnode == NULL) {
        exit(-1);
    }
    newnode->data = x;
    newnode->next = NULL;
    newnode->prev = NULL;
    return newnode;
}

void DSqListPushBack(DSL* phead, DSqListType x) {
    DSL* tail = phead->prev;
    DSL* cur = BuyDSqListNode(x);
    tail->next = cur;
    cur->prev = tail;
    cur->next = phead;
    phead->prev = cur;
}
void DSqListPrint(DSL* phead)
{
    DSL* cur = phead->next;
    while (cur !=phead)
    {
        printf("%d->", cur->data);
        cur = cur->next;
    }
    printf("NULL\n");
}
void DSqListPushFront(DSL* phead, DSqListType x)
{
    DSL* newnode = BuyDSqListNode(x);
    DSL* cur = phead->next;
    phead->next = newnode;
    newnode->prev = phead;
    newnode->next = cur;
    cur->prev = newnode;
}
void DSqListPopFront(DSL* phead)
{
    DSL* first = phead->next;
    first->next->prev = phead;
    phead->next = first->next;
    free(first);
}
void DSqListPopBack(DSL* phead)
{
    DSL* tail = phead->prev;
    tail->prev->next = phead;
    phead->prev = tail->prev;
    free(tail);
}
DSL* ListFind(DSL* phead, DSqListType x)
{
    DSL* cur = phead->next;
    while (cur !=phead)
    {
        if (cur->data == x)
        {
            return cur;
        }
        cur = cur->next;
    }
    return NULL;
}
void ListInsert(DSL* pos, DSqListType x)
{
    DSL* newnode = BuyDSqListNode(x);
    pos->prev->next = newnode;
    newnode->prev = pos->prev;
    newnode->next = pos;
    pos->prev = newnode;
}

void ListErase(DSL* pos)
{
    DSL* cur = pos->prev;
    DSL* next = pos->next;
    cur->next = next;
    next->prev = cur;
    free(pos);
}